﻿using System.Collections.Generic;
using System.Linq;
using GameSchoolApplication.Models;

namespace GameSchoolApplication.Repository
{
	public class LevelRepository
	{
        
		private GameSchoolWebContext _db = new GameSchoolWebContext();
 
		/// <summary>
		/// Returns a list of videos
		/// </summary>
		/// <returns></returns>
		public IEnumerable<Video> GetAllVideo()
		{
			return _db.Videos.ToList();
		}

		/// <summary>
		/// Returns a video by id
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public Video GetVideoById(int id)
		{
			var video = _db.Videos.Where(m => m.VideoId == id).SingleOrDefault();
			return video;
		}

		/// <summary>
		/// Add a video
		/// </summary>
		/// <param name="video"></param>
		public void AddVideo(Video video)
		{
			_db.Videos.Add(video);
			_db.SaveChanges();
			
		}

		/// <summary>
		/// Delete a video
		/// </summary>
		/// <param name="video"></param>
		public void DeleteVideo(Video video)
		{
			_db.Videos.Remove(video);
			_db.SaveChanges();
		}

		/// <summary>
		/// Add a comment to the repository
		/// </summary>
		/// <param name="comment"></param>
		public void AddCommentToVideo(Comment comment)
		{
            _db.Comments.Add(comment);
			_db.SaveChanges();
		}

		/// <summary>
		/// Add a like to a comment 
		/// </summary>
		/// <param name="like"></param>
		public void LikeComment(Like like)
		{
			_db.Likes.Add(like);
			_db.SaveChanges();
		}


        public IEnumerable<Comment> GetComments(int videoId)
        {
            var video = _db.Videos.Where(m => m.VideoId == videoId).SingleOrDefault();

            var result = video.Comments;

            return result;
        }


        public IEnumerable<Like> GetAllLikes(int commentID)
        {
           return _db.Likes.Where(like => like.CommentId == commentID);                         
        }

		/// <summary>
		/// Like a commnent
		/// </summary>
		/// <param name="superLike"></param>
		public void SuperLikeComment(SuperLike superLike)
		{
			_db.SuperLikes.Add(superLike);
			_db.SaveChanges();
		}

		/// <summary>
		/// Add a task
		/// </summary>
		/// <param name="task"></param>
		public void AddTask(Task task)
		{
			_db.Tasks.Add(task);
			_db.SaveChanges();
		}

		/// <summary>
		/// Get all test of game
		/// </summary>
		/// <param name="game"></param>
		/// <returns></returns>
		public IEnumerable<Test> GetAllTestOfGame(Game game)
		{
			var testsOfLevel = game.Levels.Select(m => m.Tests);
			return testsOfLevel as IEnumerable<Test>;
		}

		/// <summary>
		/// Return all assignments of a game
		/// </summary>
		/// <param name="game"></param>
		/// <returns></returns>
		public IEnumerable<Assignment> GetAllAssignmentOfGame(Game game)
		{
			var assignmentsOfLevel = game.Levels.Select(m => m.Assignments);
			return assignmentsOfLevel as IEnumerable<Assignment>;
		}
	}
}